package edu.upc.fib.lamsandroid.client.adapters;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import edu.upc.fib.lamsandroid.server.api.model.Lesson;
import edu.upc.fib.lamsandroid.server.api.model.Organisation;

import java.util.Hashtable;
import java.util.List;

public class LessonExpandableListAdapter extends BaseExpandableListAdapter {

    private Activity activity;
    private Hashtable<Organisation, List<Lesson>> lessonsByOrganisation;
    private Organisation[] organisations;

    public LessonExpandableListAdapter(Activity activity, Hashtable<Organisation, List<Lesson>> lessonsByOrganisation) {
        this.activity = activity;
        this.lessonsByOrganisation = lessonsByOrganisation;
        organisations = new Organisation[lessonsByOrganisation.size()];
        organisations = lessonsByOrganisation.keySet().toArray(organisations);
    }

    @Override
    public int getGroupCount() {
        return lessonsByOrganisation.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return lessonsByOrganisation.get(organisations[i]).size();
    }

    @Override
    public Object getGroup(int i) {
        return lessonsByOrganisation.get(organisations[i]);
    }

    @Override
    public Object getChild(int i, int i2) {
        return lessonsByOrganisation.get(organisations[i]).get(i2);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i2) {
        return i2;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        Organisation organisation = organisations[i];

        TextView textView = getGenericView();
        textView.setText(organisation.getName() + "  [ " + organisation.getDescription() + " ]");

        return textView;
    }

    @Override
    public View getChildView(int i, int i2, boolean b, View view, ViewGroup viewGroup) {
        Lesson lesson = (Lesson) getChild(i, i2);

        TextView textView = getGenericView();
        textView.setText(lesson.getTitle() + "  [ " + lesson.getDescription() + " ]");

        return textView;
    }

    @Override
    public boolean isChildSelectable(int i, int i2) {
        return true;
    }

    public TextView getGenericView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);

        TextView textView = new TextView(activity);
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        //textView.setTextColor(R.color.marcyred);
        // Set the text starting position
        textView.setPadding(36, 0, 0, 0);
        return textView;
    }
}
